package com.joplus.housekeeper.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.ExpandableListView;

/**
 * Created by Administrator on 2016/1/15.
 */
public class LoadMoreexListView extends ExpandableListView implements AbsListView.OnScrollListener {
    public LoadMoreexListView(Context context) {
        super(context);

        init();
    }


    public LoadMoreexListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadMoreexListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {


//        this.setLayoutAnimation(getLayoutAnimationController());
        this.setOnScrollListener(this);
    }

    private LayoutAnimationController getLayoutAnimationController() {
        AnimationSet set = new AnimationSet(false);
        Animation animation = new AlphaAnimation(0, 1);   //AlphaAnimation 控制渐变透明的动画效果
        animation.setDuration(1000);     //动画时间毫秒数
        set.addAnimation(animation);    //加入动画集合

        animation = new TranslateAnimation(1, 13, 10, 50);  //ScaleAnimation 控制尺寸伸缩的动画效果
        animation.setDuration(1000);
        set.addAnimation(animation);

        animation = new RotateAnimation(30, 10);    //TranslateAnimation  控制画面平移的动画效果
        animation.setDuration(1000);
        set.addAnimation(animation);

        animation = new ScaleAnimation(5, 0, 2, 0);    //RotateAnimation  控制画面角度变化的动画效果
        animation.setDuration(1000);
        set.addAnimation(animation);

        LayoutAnimationController controller = new LayoutAnimationController(set, 1);

        return controller;
    }


    private int lastItem;

    private int mTotalItemCount;

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        if (lastItem == mTotalItemCount && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            if (onLoadMoreListener != null) {
                onLoadMoreListener.onLoadMore();
            }
        }
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        lastItem = firstVisibleItem + visibleItemCount;

        mTotalItemCount = totalItemCount;
    }


    private LoadMoreexListView.OnLoadMoreListener onLoadMoreListener;

    public void setOnLoadMoreListener(LoadMoreexListView.OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }
}
